package org.overtheair.say.logic;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public abstract class DataProviderAncestor
{
	/** Maximum query results */
	protected static final int MAX_RESULTS = 10;

	protected static JSONArray detachDataPartFromYQLWebserviceReply(String jsonStream, String objectName)
	{
		JSONObject obj;
		JSONObject obj2;
		JSONArray arrObjExtracted;
		
		arrObjExtracted = null;
		try {
			obj = new JSONObject(jsonStream);
			obj2 = obj.optJSONObject("query");
			if (null != obj2)
			{
				obj = obj2.optJSONObject("results");
				if (null != obj)
				{
					arrObjExtracted = obj.optJSONArray(objectName);
				}
			}
		} catch (JSONException e) {
			arrObjExtracted = null;
		}
		
		return arrObjExtracted;
	}

	/**
	 * replace location and limit in the url
	 */
	protected static String replaceLocationAndLimit(String baseURL, String location, int limit)
	{
		return baseURL
			.replace("##location##", location.replace(" ", "%20"))
			.replace("##limit##", new Integer(limit).toString());
	}
	
	/**
	 * replace latitude, longitude, accuracy and limit from the url
	 */
	protected static String replaceLatitudeLongitudeAccuracyLimitgetUpcoming(String baseURL, double latitude, double longitude, int accuracy, int limit)
	{
		return baseURL
			.replace("##latitude##", new Double(latitude).toString())
			.replace("##longitude##", new Double(longitude).toString())
			.replace("##accuracy##", new Integer(accuracy).toString())
			.replace("##limit##", new Integer(limit).toString());
	}

}
